<template>
  <div class="app-container">
    <div class="el-toolbar">
      <div class="el-toolbar-bod">
        <a href="http://localhost:8202/admin/cmn/dict/exportData" target="_blank">
          <el-button type="text" @click="exportData">
            导出
          </el-button>
        </a>
        <el-button type="text" @click="importData">
          导入
        </el-button>
      </div>
    </div>

    <el-table
      :data="list"
      style="width: 100%"
      row-key="id"
      border
      lazy
      :load="getChildrens"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column prop="name" label="名称" width="180" />
        <el-table-column prop="value" label="值" width="180" />
        <el-table-column prop="dictCode" label="编码" width="180" />
    </el-table>

 
    <el-dialog title="导入" :visible.sync="dialogImportVisible" width="480px">
      <el-form label-position="right" label-width="170px" :model="form">
        <el-form-item label="文件">
          <el-upload 
          :multiple="false" 
          :on-success="onUploadSuccess"
          :action="'http://localhost:8202/admin/cmn/dict/importData'" 
          class="upload-demo">
          
          <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form> 
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogImportVisible = false">取 消</el-button> 
      </div>  
    </el-dialog>
  </div>

</template>

<script>
import { dictList } from '@/api/dict'

export default {
  data() {
    return {
      list: [],     
      dialogImportVisible: false
    }
  },
  created() {
    this.getList(1)
  },
  methods: {
    getList(id) {
      this.listLoading = true
      dictList(id)
        .then(
          response => {
            console.log(response)
            this.list = response.data
            this.listLoading = false
          }
        ).catch(
          error => {
            console.log(error)
            this.listLoading = false
          }
        )
    },
    getChildrens(tree, treeNode, resolve) { 
      this.listLoading = true
      dictList(tree.id)
        .then(
          response => {
            console.log(response)
            resolve(response.data)
          }
        ).catch(
          error => {
            console.log(error)
            this.listLoading = false
          }
        )
    },
    exportData() {
      window.location.href = "http://localhost:8202/admin/cmn/dict/exportData"  
    },
    importData() {
      this.dialogImportVisible = true
    },
    onUploadSuccess() {
      this.dialogImportVisible = false
      this.getList(1)
    }

  }
}
</script>
